package com.cch.demo.sys.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.JndiDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.cch.demo.bus.service.impl.SqlMapperServiceImpl;
import com.cch.demo.sys.domain.AdataSource;
import com.cch.demo.sys.domain.User;
import com.cch.demo.sys.dto.DataSourceDTO;
import com.cch.demo.sys.service.UserService;
import com.cch.demo.sys.service.impl.AdataSourceServiceImpl;
import com.cch.demo.sys.service.impl.UserServiceImpl;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.vo.UserVo;

//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
@RequestMapping("/datasources")
//@Api(tags = "添加删除数据源")
public class LoadController {

    private final DataSource dataSource;
    private final DataSourceCreator dataSourceCreator;
    private final BasicDataSourceCreator basicDataSourceCreator;
    private final JndiDataSourceCreator jndiDataSourceCreator;
    private final DruidDataSourceCreator druidDataSourceCreator;
    private final HikariDataSourceCreator hikariDataSourceCreator;

    @Autowired
    private AdataSourceServiceImpl dataSourceServiceImpl;
    
    @Autowired
    private UserServiceImpl userServiceImpl;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private SqlMapperServiceImpl sqlMapperServiceImpl;
    
    
    @GetMapping
//    @ApiOperation("获取当前所有数据源")
    public Set<String> now() {
    	List<AdataSource> selectList = dataSourceServiceImpl.selectList(null);
    	System.out.println(selectList.get(0).getUrl());
    	System.out.println(selectList.get(0).getName());
    	
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        return ds.getCurrentDataSources().keySet();
    }

    @PostMapping("/add")
//    @ApiOperation("通用添加数据源（推荐）")
    public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPollName(), dataSource);
        return ds.getCurrentDataSources().keySet();
    }

    @PostMapping("/addBasic")
//    @ApiOperation(value = "添加基础数据源", notes = "调用Springboot内置方法创建数据源，兼容1,2")
    public Set<String> addBasic(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = basicDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPollName(), dataSource);
        return ds.getCurrentDataSources().keySet();
    }

    @PostMapping("/addJndi")
//    @ApiOperation("添加JNDI数据源")
    public Set<String> addJndi(String pollName, String jndiName) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = jndiDataSourceCreator.createDataSource(jndiName);
        ds.addDataSource(pollName, dataSource);
        return ds.getCurrentDataSources().keySet();
    }

    @PostMapping("/addDruid")
//    @ApiOperation("基础Druid数据源")
    public Set<String> addDruid(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = druidDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPollName(), dataSource);
        return ds.getCurrentDataSources().keySet();
    }

    @PostMapping("/addHikariCP")
//    @ApiOperation("基础HikariCP数据源")
    public Set<String> addHikariCP(@Validated @RequestBody DataSourceDTO dto) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        BeanUtils.copyProperties(dto, dataSourceProperty);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
        ds.addDataSource(dto.getPollName(), dataSource);
        return ds.getCurrentDataSources().keySet();
    }

    @DeleteMapping
//    @ApiOperation("删除数据源")
    public String remove(String name) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        ds.removeDataSource(name);
        return "删除成功";
    }
    
//    @GetMapping("/list")
//    public Set<String> list() {
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        return ds.getCurrentDataSources().keySet();
//    }
    
    @GetMapping("/addtest")
    public Set<String> addTest() {
    	
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        List<AdataSource> selectList = dataSourceServiceImpl.selectList(null);
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
        for (int i = 0; i < selectList.size(); i++) {
        	AdataSource dataSource2 = selectList.get(i);
        	DataSourceDTO dto = new DataSourceDTO();
	          dto.setPollName(dataSource2.getName());
	          dto.setUrl(dataSource2.getUrl());
	          dto.setUsername(dataSource2.getUserName());
	          dto.setPassword(dataSource2.getPassWord());
	          dto.setDriverClassName(dataSource2.getDatabasetype());
	        BeanUtils.copyProperties(dto, dataSourceProperty);
	        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
	        ds.addDataSource(dto.getPollName(), dataSource);
//	        break;
		}
//        DataSourceDTO dto = new DataSourceDTO();
//        dto.setPollName("test2");
//        dto.setUrl("jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false");
//        dto.setUsername("root");
//        dto.setPassword("root");
//        dto.setDriverClassName("com.mysql.jdbc.Driver");
//        
//        BeanUtils.copyProperties(dto, dataSourceProperty);
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        DataSource dataSource = hikariDataSourceCreator.createDataSource(dataSourceProperty);
//        ds.addDataSource(dto.getPollName(), dataSource);
//        
//        List<User> selectList = userServiceImpl.list(null,"test2");
//        System.out.println(selectList);
        return ds.getCurrentDataSources().keySet();
    }
    
    @GetMapping("/list1")
    public List<User> list1() {
    	List<User> selectList = userServiceImpl.list(null,"1");
        System.out.println(selectList);
        return selectList;
    }
    
    @GetMapping("/list2")
    public List<User> list2() {
    	List<User> selectList = userServiceImpl.list(null,"2");
        System.out.println(selectList);
        return selectList;
    }
    
    @GetMapping("/executeSql")
    public DataGridView executeSql(String DSname, String sql) {
    	System.out.println(sql);
    	List<LinkedHashMap<String, Object>> executeSql = sqlMapperServiceImpl.executeSql(DSname, sql);
    	int size = executeSql.size();
    	Long l = Long.valueOf(size);
    	return new DataGridView(l,executeSql);
    }
    
    @GetMapping("/test1")
    public DataGridView test1(String DSname, String sql) {
    	sql = "SELECT * FROM bus_goods";
    	DSname = "test1";
    	List<LinkedHashMap<String, Object>> executeSql = sqlMapperServiceImpl.executeSql(DSname, sql);
    	int size = executeSql.size();
    	Long l = Long.valueOf(size);
    	return new DataGridView(l,executeSql);
    }
    
    
//    @GetMapping("/testAdd1")
//    public boolean testAdd1() {
//    	UserVo userVo = new UserVo();
//    	userVo.setName("1");
//    	return userService.addUser("test1", userVo);
//    }
//    
////    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
//    @GetMapping("/testAdd2")
//    public boolean testAdd2() {
//    	UserVo userVo = new UserVo();
//    	userVo.setName("2");
//    	return userService.addUser("test2", userVo);
//    }
    
//    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
//    @Transactional
//    @GetMapping("/testAdd3")
//    public boolean testAdd3() {
//    	UserVo userVo2 = new UserVo();
//    	userVo2.setName("2");
//    	boolean addUser2 = userService.addUser("test2", userVo2);
//    	System.out.println(addUser2);
//    	
//    	UserVo userVo1 = new UserVo();
//    	userVo1.setName("1");
//    	boolean addUser1 = userService.addUser("test1", userVo1);
//    	System.out.println(addUser1);
////    	int a = 3;
////    	int b = 0;
////    	int c = a/b;
//    	return true;
//    }
}
